<?php
namespace app\logic;

use app\common\corelib\CacheHelper;
use app\models\Favorite;
use app\models\Goods;
use app\models\GoodsSku;

class GoodsLogic
{
    public function search($keyword,$orderBy,$category_id,$page,$limit){
        if($category_id){
            $goods = Goods::find()
                ->select('g.goods_id,g.goods_pic,g.goods_name,g.goods_price')
                ->from('{{%goods}} g')
                ->leftJoin('{{%goods_category}} gc','gc.goods_id=g.goods_id')
                ->where(['LIKE','goods_name',$keyword])
                ->andWhere(['or',['gc.first_category_id'=>$category_id],['gc.second_category_id'=>$category_id]])
                ->andWhere(['g.status'=>1]);
        }else{
            $goods = Goods::find()->select('goods_id,goods_pic,goods_name,goods_price')->where(['LIKE','goods_name',$keyword]);
        }
        if($orderBy==1){
            $goods->orderBy('goods_sort asc,update_time desc');
        }elseif($orderBy==2){
            $goods->orderBy('sales_volume desc,goods_sort asc,update_time desc');
        }elseif($orderBy==3){
            $goods->orderBy('goods_price asc,goods_sort asc,update_time desc');
        }elseif($orderBy==4){
            $goods->orderBy('goods_price desc,goods_sort asc,update_time desc');
        }
        return $goods->offset($limit*($page-1))->limit($limit)->asArray()->all();
    }

    public function getAllGoodsByCategory($orderBy,$category_id,$page,$limit){
        $goods = Goods::find()
            ->select('g.goods_id,g.goods_pic,g.goods_name,g.goods_price')
            ->from('{{%goods}} g')
            ->leftJoin('{{%goods_category}} gc','gc.goods_id=g.goods_id')
            ->where(['or',['gc.first_category_id'=>$category_id],['gc.second_category_id'=>$category_id]])
            ->andWhere(['g.status'=>1]);
        if($orderBy==1){
            $goods->orderBy('goods_sort asc,update_time desc');
        }elseif($orderBy==2){
            $goods->orderBy('sales_volume desc,goods_sort asc,update_time desc');
        }elseif($orderBy==3){
            $goods->orderBy('goods_price asc,goods_sort asc,update_time desc');
        }elseif($orderBy==4){
            $goods->orderBy('goods_price desc,goods_sort asc,update_time desc');
        }
        return $goods->offset($limit*($page-1))->limit($limit)->asArray()->all();
    }

    public function getAllGoodsByHot(){
        return Goods::find()->select('goods_id,goods_pic,goods_name,goods_price')
            ->where(['is_hot'=>1,'status'=>1])
            ->orderBy('goods_sort asc')
            ->asArray()->all();
    }

    public function getAllGoods($page,$limit){
        return Goods::find()->select('goods_id,goods_pic,goods_name,goods_price')
            ->where(['status'=>1])
            ->orderBy('goods_sort asc, update_time desc')
            ->offset($limit*($page-1))->limit($limit)
            ->asArray()->all();
    }

    public function getAllGoodsByNew($page,$limit){
        return Goods::find()->select('goods_id,goods_pic,goods_name,goods_price')
            ->where(['is_new'=>1,'status'=>1])
            ->orderBy('goods_sort asc')
            ->offset($limit*($page-1))->limit($limit)
            ->asArray()->all();
    }

    public function getGoods($goods_id){
        return Goods::find()
            ->select('goods_pics,goods_name,simple_desc,goods_price,yanxuan_price,sales_volume,attr_list,goods_detail')
            ->where(['goods_id'=>$goods_id,'status'=>1])
            ->asArray()->one();
    }

    public function getFavorite($user_id,$goods_id){
        return Favorite::find()->where(['user_id'=>$user_id,'goods_id'=>$goods_id])->asArray()->one();
    }

    public function getGoodsSku($goods_id){
        return GoodsSku::find()
            ->select('sku_id,yx_sku_id,sku_price,yanxuan_price,sku_pic,sku_spec,inventory')
            ->where(['goods_id'=>$goods_id])
            ->asArray()->all();
    }

    public function itemChangeCallback($itemChangeResult){
        $itemChangeResult = json_decode($itemChangeResult,true);
        $itemChangeResult = $itemChangeResult['itemChangeDetailList'];
        foreach($itemChangeResult AS $value){
            $changeDetail = json_decode($value['changeDetail'],true);
            if($value['type']==2){

                $yx_sku_id = $changeDetail['skuId'];
                $gs = GoodsSku::findOne(['yx_sku_id'=>$yx_sku_id]);
                if($changeDetail['skuChangeCode'] == "item.skuList.channelSkuDetail.status"){
                    $off = CacheHelper::get('off');
                    if($changeDetail['value']==0){
                        $off[] = $yx_sku_id;
                        $gs->inventory_status = 0;
                        $gs->save();
                        $allgs = GoodsSku::find()->where(['goods_id'=>$gs->goods_id])->asArray()->all();
                        $needupdategoods = true;
                        foreach($allgs AS $k=>$v){
                            if($v['inventory_status']!=0){
                                $needupdategoods = false;
                            }
                        }
                        if($needupdategoods){
                            $goods = Goods::findOne(['goods_id'=>$gs->goods_id]);
                            $goods->buy_status = 0;
                            $goods->status = 0;
                            $goods->save();
                        }

                    }elseif($changeDetail['value']==1){
                        $key = array_search($yx_sku_id, $off);
                        if ($key !== false)
                            array_splice($off, $key, 1);
                        $gs->inventory_status = 1;
                        $gs->save();
                    }
                    CacheHelper::set('off',$off);

                }elseif($changeDetail['skuChangeCode'] == "item.skuList.channelSkuDetail.channelPrice"){
                    $gs->sku_price = $changeDetail['value'];
                    $gs->channel_price = $changeDetail['value'];
                    $gs->save();
                }

            }elseif($value['type']==1){
                if($changeDetail['itemChangeCode'] == 'item.categoryPathList.id'){
                    CacheHelper::set('local_first_category_temp',null);
                }/*else{
                    CacheHelper::set("local_goods",null);
                }*/
            }
        }
    }

    public function inventoryCheck($skuCheck){
        $skuCheck = json_decode($skuCheck,true);
        $skuChecks = $skuCheck['skuChecks'];
        foreach($skuChecks AS $key=>$value){
            $gs = GoodsSku::findOne(['yx_sku_id'=>$value['skuId']]);
            $gs->inventory = $value['count'];
            if($value['count']==0){
                $gs->inventory_status = 0;
            }
            $gs->save();
            $allgs = GoodsSku::find()->where(['goods_id'=>$gs->goods_id])->asArray()->all();
            $needupdategoods = true;
            foreach($allgs AS $k=>$v){
                if($v['inventory_status']!=0){
                    $needupdategoods = false;
                }
            }
            if($needupdategoods){
                $goods = Goods::findOne(['goods_id'=>$gs->goods_id]);
                $goods->buy_status = 0;
                $goods->status = 0;
                $goods->save();
            }
        }
    }

    public function skuAlarm($closeAlarmSkus){
        $closeAlarmSkus = json_decode($closeAlarmSkus,true);
        foreach($closeAlarmSkus AS $key=>$value){
            $gs = GoodsSku::findOne(['yx_sku_id'=>$value['skuId']]);
            $gs->inventory = $value['inventory'];
            $gs->inventory_status = 0;
            $gs->save();
            $allgs = GoodsSku::find()->where(['goods_id'=>$gs->goods_id])->asArray()->all();
            $needupdategoods = true;
            foreach($allgs AS $k=>$v){
                if($v['inventory_status']!=0){
                    $needupdategoods = false;
                }
            }
            if($needupdategoods){
                $goods = Goods::findOne(['goods_id'=>$gs->goods_id]);
                $goods->buy_status = 0;
                $goods->status = 0;
                $goods->save();
            }
        }
    }

    public function skuReopen($reopenSkus){
        $reopenSkus = json_decode($reopenSkus,true);
        foreach($reopenSkus AS $key=>$value){
            $gs = GoodsSku::findOne(['yx_sku_id'=>$value['skuId']]);
            $gs->inventory = $value['inventory'];
            $gs->inventory_status = 1;
            $gs->save();
            /*$goods = Goods::findOne(['goods_id'=>$gs->goods_id]);
            $goods->buy_status = 1;
            $goods->status = 1;
            $goods->save();*/
        }
    }

}
